using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using System.Collections.Specialized;
using EntLibContrib.Validation.Configuration;

namespace EntLibContrib.Validation
{

    [ConfigurationElementType(typeof(ExternallyConfiguredObjectValidatorData))]
    public class ExternallyConfiguredObjectValidator : Validator
    {
        private string targetRuleset;
        private string targetConfigurationFile;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public ExternallyConfiguredObjectValidator(string ruleSet, string configurationFilePath)
            : base(null, null)
        {
            this.targetRuleset = ruleSet;
            this.targetConfigurationFile = configurationFilePath;
        }

        /// <summary>
        /// Gets the message template to use when logging results no message is supplied.
        /// </summary>
        protected override string DefaultMessageTemplate
        {
            get { return null; }
        }

        protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate != null)
            {
                Type targetType = objectToValidate.GetType();

                Validator v = ValidationFactory.CreateValidator(targetType, targetRuleset, new FileConfigurationSource(targetConfigurationFile));
                v.Validate(objectToValidate, validationResults);
            }
        }
    }
}
